package vv;
import java.io.*;
import java.util.*;

public class PriceCalculator {
    public static void main(String[] args) {
        File file = new File("prices.txt");
        Scanner sc = null;
        int count = 0;
        double sum = 0;

        try {
            sc = new Scanner(file);
            sc.useDelimiter("[^0-9]+"); // 使用非数字字符作为分隔符

            while (sc.hasNextDouble()) {
                double price = sc.nextDouble();
                count++;
                sum += price;
                System.out.println(price);
            }

            double average = sum / count;
            String str = String.format("%.3f", average);
            System.out.println("平均价格：" + str);

            // 追加写入平均价格到原文件
            BufferedWriter writer = new BufferedWriter(new FileWriter(file, true));
            writer.newLine(); // 新建一行
            writer.write("平均价格：" + str);
            writer.close();

        } catch (Exception exp) {
            System.out.println(exp);
        } finally {
            if (sc != null) {
                sc.close();
            }
        }
    }
}
